package com.steshov.trainer.dao;

import android.content.Context;
import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.steshov.trainer.activities.ExerciseActivity;
import com.steshov.trainer.utils.App;
import com.steshov.trainer.utils.DB_Utils;

public class ExerciseDAO {

	SQLiteDatabase db;

	{
		db = DB_Utils.db;
	}

	public void peristExercise(Context context, String name) {
		try {
			db.execSQL("INSERT INTO " + DB_Utils.EXERCISE_TAB_NAME
					+ " VALUES (null,'" + name + "');");
			App.msg(context, "An exercise is created.");
		} catch (SQLiteConstraintException e) {
			App.msg(context, "Such kind of training exists already!");
		} catch (Exception e) {
			App.msg(context, e.toString());
		}
	}

	public String[] getExercises(ExerciseActivity context) {
		try {
			return DB_Utils.convertCursorToArray("Select name from "
					+ DB_Utils.EXERCISE_TAB_NAME);
		} catch (Exception e) {
			Log.d("db", e.toString());
			App.msg(context, e.toString());
		}
		return new String[] {};
	}

	public void removeExercise(ExerciseActivity context, String name) {
		try {
			db.execSQL("DELETE FROM " + DB_Utils.EXERCISE_TAB_NAME
					+ " Where name='" + name + "';");
		} catch (Exception e) {
			App.msg(context, e.toString());
		}

	}
}
